package com.common.base.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.widget.AppCompatImageView
import com.common.base.R


/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：1/2/2023
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class SCheckBox @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
) :
    AppCompatImageView(context, attrs, defStyle), View.OnClickListener {
    private var checkedImg = 0
    private var unCheckedImg = 0
    private var isChecked = false
    private var notClickListener = false
    private var listener: OnCheckedChangeListener? = null

    init {
        initView(context, attrs)
    }

    fun setOnCheckedChangeListener(listener: OnCheckedChangeListener?) {
        this.listener = listener
    }

    fun checked() {
        if (isChecked) {
            return
        }
        isChecked = true
        setImageResource(checkedImg)
        notifyListener(isChecked)
    }

    fun unChecked() {
        if (!isChecked) {
            return
        }
        isChecked = false
        setImageResource(unCheckedImg)
        notifyListener(isChecked)
    }

    fun unCheckedNotNotify() {
        if (!isChecked) {
            return
        }
        isChecked = false
        setImageResource(unCheckedImg)
    }

    fun setChecked(isChecked: Boolean) {
        if (isChecked) {
            checked()
        } else {
            unChecked()
        }
    }

    /**
     * 设置复选框状态
     * 该方法不会触发监听事件
     * @param isChekced
     */
    fun initChecked(isChekced: Boolean) {
        isChecked = isChekced
        if (isChekced) {
            setImageResource(checkedImg)
        } else {
            setImageResource(unCheckedImg)
        }
    }

    fun toggle() {
        if (isChecked) {
            unChecked()
        } else {
            checked()
        }
    }

    fun isChecked(): Boolean {
        return isChecked
    }

    private fun notifyListener(isChecked: Boolean) {
        listener?.onCheckedChange(this, isChecked)
    }

    private fun initView(context: Context, attrs: AttributeSet?) {
        val typed = context.obtainStyledAttributes(
            attrs,
            R.styleable.SCheckBox
        )
        checkedImg = typed.getResourceId(
            R.styleable.SCheckBox_ckCheckedImg,
            NOT_FOUND
        )
        unCheckedImg = typed.getResourceId(
            R.styleable.SCheckBox_ckUnCheckedImg,
            NOT_FOUND
        )
        notClickListener = typed.getBoolean(
            R.styleable.SCheckBox_ckNotClickListener,
            false
        )
        if (checkedImg == NOT_FOUND || unCheckedImg == NOT_FOUND) {
            throw RuntimeException("[SCheckBox] img resource is not found")
        }
        typed.recycle()
        setImageResource(unCheckedImg)
        if (!notClickListener) {
            setOnClickListener(this)
        }
    }

    interface OnCheckedChangeListener {
        fun onCheckedChange(view: View?, isChecked: Boolean)
    }

    override fun onClick(v: View?) {
        toggle()
    }

    companion object {
        private const val NOT_FOUND = -1
    }
}